
# generate table 2

# load packages
library(rio) # load data
library(tidyverse) # data manipulation
library(lmtest) # cluster standard errors
library(sandwich) # cluster standard errors
library(stargazer) # generate tables

# set working directory
setwd("~/replication_files/")

# load data for analysis
data_with_dictionary <- import("data/full_data.csv") %>%
  mutate(across(c(imf_program,field_discovery,grad_school_econ_usa,iso3c,year), as.factor)) # treat factors as factors

# model 1, table 2
ols1 <- lm(policy_passage ~ resource_mentions_absolute_lag + 
             previous_policy +  grad_school_econ_usa + fdi_performance_index_lag +
             imf_program + price_crudeoil_lag + price_crudeoil_difference + 
             resource_rents_lag + log_gdp_per_capita_lag + gdp_growth_lag + field_discovery_lag + 
             polyarchy + left_executive + protest + year + iso3c, data = data_with_dictionary) 

# cluster standard errors
ols1_r <- ols1 %>% 
  coeftest(vcovHC(ols1, type = 'HC0', cluster =  ~ iso3c))

# generate table 2
stargazer(ols1_r, # type = "text",
          omit = c("iso3c","year"), 
          dep.var.labels = "Policy Passage",
          covariate.labels = c("Natural Resource Term Frequency, t--1", "Previous Policy Passage = 1",
                               "Technocratic Finance Minister = 1", "FDI Performance Index",
                               "IMF Program = 1", "Crude Oil Price, t--1", "Crude Oil Price, Delta",
                               "Resource Rents, t--1", "Log GDP Per Capita, t--1", "GDP Growth, t--1", "Field Discovery = 1, t--1",
                               "Polyarchy", "Left Executive", "Protest Count"),
          keep.stat = c("n", "rsq", "f"))

# extract fit stats
stargazer(ols1, # type = "text",
          omit = c("iso3c","year"), 
          dep.var.labels = "Policy Passage",
          covariate.labels = c("Natural Resource Term Frequency, t--1", "Previous Policy Passage = 1",
                               "Technocratic Finance Minister = 1", "FDI Performance Index",
                               "IMF Program = 1", "Crude Oil Price, t--1", "Crude Oil Price, Delta",
                               "Resource Rents, t--1", "Log GDP Per Capita, t--1", "GDP Growth, t--1", "Field Discovery = 1, t--1",
                               "Polyarchy", "Left Executive", "Protest Count"),
          keep.stat = c("n", "rsq", "f"))
